VERSION 1.0 CLASS
BEGIN
  MultiUse = -1  'True
  Persistable = 0  'NotPersistable
  DataBindingBehavior = 0  'vbNone
  DataSourceBehavior  = 0  'vbNone
  MTSTransactionMode  = 0  'NotAnMTSObject
END
Attribute VB_Name = "DieselClass"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = True
Attribute VB_PredeclaredId = False
Attribute VB_Exposed = False
' Result tags
'AverageValue
'Opacity(n)         Opacity value from  n times
'KValue(n)            K value from n times
'Max(n)               Max speed value from n times
'Min1                 Min speed value from n times


' Other tags, show current status window
'MeasureScreen:     show current screen (Stable, Accelerate....)
'Action             Accelerate, Stop to inform tester action
'MeasureTimes       The number of action from tester
'
'-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

Option Explicit
Const CmdNonSignal As Byte = &HFF
Const CmdNone As Byte = 0
Const CmdGetingStatus As Byte = &H1
Const CmdGetingMesuringVal As Byte = &H2
Const CmdGetingStableStatus As Byte = &H3
Const CmdGetingAccelarateVal As Byte = &H4
Const CmdPerformingLinearity As Byte = &H5
Const CmdChangeMesurementScr As Byte = &H6
Const CmdChangeStableStatusScr As Byte = &H7
Const CmdChangeAccelerateScr As Byte = &H8
Const CmdStartOrStopStableStatus As Byte = &H9
Const CmdOilTempValue As Byte = &H10
Const CmdPerformOneTimeAccelerateTrigger As Byte = &HB
Const CmdAcceleratingCurve As Byte = &HF
Const CmdMainAccelerating As Byte = &HE

Const SCREEN_STABLE As Byte = 0
Const SCREEN_WARMUP As Byte = 1
Const SCREEN_MESURE As Byte = 2
Const SCREEN_ACCELERATE As Byte = 3
Const SCREEN_MENU As Byte = 3

Const SCREEN_STABLE_NAME As String = "Stable"
Const SCREEN_WARMUP_NAME As String = "Warmup"
Const SCREEN_MESURE_NAME As String = "Measure"
Const SCREEN_ACCELERATE_NAME As String = "Accelerate"
Const SCREEN_MENU_NAME As String = "Menu"



Const ACTION_START As String = "Nhan ga"
Const ACTION_STOP As String = "Nha ga"
Const ACTION_KEEP As String = "Giu chan ga..."
Const Nresult As Integer = 3

'----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
' Objects And Setting
Private WithEvents MSComm1 As MSComm
Attribute MSComm1.VB_VarHelpID = -1
Private WithEvents mobjTmr As vbAPITimerTools.APITimer
Attribute mobjTmr.VB_VarHelpID = -1
Public MSCommPort As Integer
Private prMSComSettings As String
Public IsRunning As Boolean
Private prSendingQueue As New Collection
Private prInComePackets As New Collection
Private inputBuffer As Variant
Private IsNewPacketArried As Boolean
Private iMeasureTimes As Integer


'----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
'Flag
Private isCommBusy As Boolean
Private IsTimerBusy As Boolean

'private properties'----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Private prAddress As Byte
Private prCurrentScreen As Byte

'------------Class Properties --------------------------------------------------------------------------------------------------------------------------------------------------
Public Property Set MSCommObject(ByVal value As MSComm)
    Set MSComm1 = value
End Property

Public Property Set TimerObject(ByVal value As APITimer)
    Set mobjTmr = value
End Property

Public Property Let HostAddress(ByVal value As Byte)
    If IsRunning Then
        MsgBox "have to stop before changing host address"
        Exit Property
    End If
    prAddress = value
End Property
Public Property Let MSComSettings(ByVal value As String)
    If IsRunning Then
        MsgBox "have to stop before MSComSettings"
        Exit Property
    End If
    prMSComSettings = value
End Property
Public Property Get MSComSettings() As String
    MSComSettings = prMSComSettings
End Property


'------------End Class Properties --------------------------------------------------------------------------------------------------------------------------------------------------

'---------- Public Method--------------------------------------------------------------------------------------------------------------------------
Public Sub StartRun()
    '''TODO:
    '       Open Rs232Port If not
    '       Start Timer
    InitRs232
    IsRunning = True
    
    ' init data
    iMeasureTimes = 0
    SetTag "MeasureTimes", ""
    SetTag "Action", ""
    SetTag "MeasureScreen", ""

    prSendingQueue.Add CmdGetingStatus
    
    
    StartTimer
End Sub

Public Sub StopRun()
    '''TODO:
    '       Stop Timer
    StopTimer
    IsRunning = False
End Sub
Private Sub SetRs232Output(data() As Byte)
        MSComm1.Output = data
End Sub



Public Sub SendCmd(cmdCode As Byte)
    Dim data(7) As Byte
    Dim chkSum As Long
    data(0) = Asc("f")      'ascii code of f (102)
    data(1) = Asc("t")      'ascii code of t (116)
    data(2) = Asc("y")      'ascii code of y (121)
    data(3) = prAddress    'Address
    data(4) = &H2           'CommandLength now all in 02H
    data(5) = cmdCode
    
    'check sum = 256 -(102+116+121 +address +commandLegth + commandCode) mod 256
    chkSum = 256 - (102 + 116 + 121 + data(3) + data(4) + data(5)) Mod 256
        
    ' data(6) = 256 - (data(0) + data(1) + data(2) + data(3) + data(4) + data(5)) Mod 256
    data(6) = CByte(chkSum)
    'send to COM port
    SetRs232Output data
End Sub



Private Sub InitRs232()
    If MSCommPort = 0 Then MSCommPort = 1
    If MSComSettings = "" Then MSComSettings = "9600, n, 8, 1"
    Set MSComm1 = Nothing
    Set MSComm1 = New MSComm
    
    If Not MSComm1.PortOpen Then
        MSComm1.CommPort = MSCommPort  '1  0-> 254
        MSComm1.PortOpen = True
        MSComm1.InputMode = comInputModeBinary     ' ????
        MSComm1.Settings = MSComSettings      'Baurate,Parity,Databit,Stopbit
        MSComm1.RThreshold = 1
    End If
End Sub
Private Sub StartTimer()
    Dim lngInterval     As Long
    lngInterval = 1000
    If mobjTmr Is Nothing Then
        Set mobjTmr = New vbAPITimerTools.APITimer
    End If
    
    
    If lngInterval > 0 Then
        mobjTmr.StartTimer lngInterval
        
    Else
        MsgBox "Please enter a valid interval.", vbExclamation, "API Timer Interval"
    End If
End Sub

Private Sub StopTimer()
    mobjTmr.StopTimer
    'IsTimerBusy = False
End Sub



Public Sub Release()
    If Not MSComm1 Is Nothing And MSComm1.PortOpen Then
    MSComm1.PortOpen = False
    End If
    Set MSComm1 = Nothing
    Set mobjTmr = Nothing
End Sub

Private Sub Class_Initialize()
    ''TOTO: init class
    prAddress = 1
    IsRunning = False
    IsNewPacketArried = True
End Sub


'--------------Utilities Function-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Public Function hex2ascii(ByVal hextext As String) As String
    Dim Y As Integer
    Dim num As Integer
    Dim value As Integer
    
    For Y = 1 To Len(hextext)
        num = Mid(hextext, Y, 2)
        value = value & Chr(Val("&h" & num))
        Y = Y + 1
    Next Y
    hex2ascii = value
End Function



Public Function ByteArrayToString(bytArray() As Byte) As String
    Dim i As Integer
    Dim j As Integer
    Dim retStr As String
    Dim strTemp As String
    Dim IsVarArrayEmpty As Boolean
    
    On Error Resume Next
        i = UBound(bytArray)
    If Err.Number = 0 And i > 0 Then
        IsVarArrayEmpty = False
    Else
        IsVarArrayEmpty = True
    End If

    If IsVarArrayEmpty Then
        ByteArrayToString = ""
        Exit Function
    Else
     For j = 0 To i
        strTemp = Hex$(bytArray(j))
        If Len(strTemp) < 2 Then strTemp = "0" & strTemp
        retStr = retStr & strTemp
     Next j
     ByteArrayToString = retStr
    End If
 End Function
'--------------Utilities Function-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

Private Sub Class_Terminate()
    Set prSendingQueue = Nothing
End Sub

'--------------Event Handle Functions -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

'Timer On Run
Public Sub mobjTmr_Refresh()
    'Process InComePacket
    If prInComePackets.Count > 0 Then
        Dim packet() As Byte
        packet = prInComePackets.Item(1)
        prInComePackets.Remove (1)
        HandleInput packet
    End If

    If IsTimerBusy = True Then Exit Sub
    'mobjTmr.StopTimer
    IsTimerBusy = True
    'Do While IsTimerBusy
       ' On Error GoTo ErrHandler
        If Not isCommBusy Then
            
            '''TODO: timer code
            'WriteLog "mobjTmr_Refresh"
            
            If prSendingQueue.Count > 0 Then
                Dim commandToSend As Byte
                Dim readyToSend As Boolean
                
                commandToSend = prSendingQueue.Item(1)
                
                readyToSend = (commandToSend = CmdAcceleratingCurve And prCurrentScreen = SCREEN_ACCELERATE) Or _
                              (commandToSend = CmdGetingStableStatus And prCurrentScreen = SCREEN_STABLE) Or _
                              (commandToSend = CmdGetingAccelarateVal And prCurrentScreen = SCREEN_STABLE) Or _
                              (commandToSend = CmdMainAccelerating And prCurrentScreen = SCREEN_STABLE)
                ' ToDo: will be removed
                readyToSend = True
                If readyToSend Then
                    SendCmd commandToSend
                End If
                prSendingQueue.Remove (1)
            Else
                SendCmd CmdGetingStatus ' make sure alway send request to instrument if Idle
            End If
            
        End If
    
    
    
    
    
    IsTimerBusy = False
    Exit Sub
ErrHandler:
        If Err.Number > 0 Then
             WriteLog "mobjTmr_Refresh >> ERROR: Err.Number:" & Err.Number & " Message:" & Err.Description
        End If
        IsTimerBusy = False
End Sub

'On Comm Events
Private Sub MSComm1_OnComm()
     isCommBusy = True
     'Dim InBuff As String
     Dim nextBuff As Variant
     Dim Temp As Variant
         Select Case MSComm1.CommEvent
         ' Handle each event or error by placing
         ' code below each case statement.

         ' This template is found in the Example
         ' section of the OnComm event Help topic
         ' in VB Help.

         ' Errors
            'Case comEventBreak   ' A Break was received.
            'Case comEventCDTO    ' CD (RLSD) Timeout.
            'Case comEventCTSTO   ' CTS Timeout.
            'Case comEventDSRTO   ' DSR Timeout.
            'Case comEventFrame   ' Framing Error.
            'Case comEventOverrun ' Data Lost.
            'Case comEventRxOver  ' Receive buffer overflow.
            'Case comEventRxParity   ' Parity Error.
            'Case comEventTxFull  ' Transmit buffer full.
            'Case comEventDCB     ' Unexpected error retrieving DCB]

         ' Events
            'Case comEvCD   ' Change in the CD line.
            'Case comEvCTS  ' Change in the CTS line.
            'Case comEvDSR  ' Change in the DSR line.
            'Case comEvRing ' Change in the Ring Indicator.
            
            Case comEvReceive ' Received RThreshold # of chars.
                'Dim InBuff As String
                nextBuff = MSComm1.Input
                Dim logbuffer() As Byte
                logbuffer = nextBuff
                If Not IsArray(nextBuff) Then
                    WriteLog "Receive wrong data:Input is not an array"
                    isCommBusy = False
                    Exit Sub
                End If
                
                Dim llen As Integer
                On Error Resume Next
                llen = UBound(nextBuff)
                If Err.Number <> 0 Then
                    WriteLog "Receive wrong data: empty Input"
                    isCommBusy = False
                End If
                
                WriteLog "Received array = " & ByteArrayToString(logbuffer)
                If IsNewPacketArried Then
                    IsNewPacketArried = False
                    inputBuffer = nextBuff
                Else
                    inputBuffer = joinArray(inputBuffer, nextBuff)
                End If
                
                'TODO checking response packet from intrustment
                Dim buffer() As Byte
                buffer = inputBuffer
                
                ' Check error
                If UBound(buffer) < 4 Then
                    'WriteLog "Invalid Address.Reset Packet"
                    IsNewPacketArried = False
                    inputBuffer = Empty
                    isCommBusy = False
                    Exit Sub
                End If
                
                Dim tAddr, tLen, tCmdCode, tPacketLen
                If buffer(0) <> prAddress Then
                    WriteLog "Invalid Address.Reset Packet"
                    IsNewPacketArried = True
                    inputBuffer = Empty
                    isCommBusy = False
                    Exit Sub
                End If
                ' end check error
                
                If buffer(1) = &H3 And buffer(2) = &HEC And buffer(3) = CmdAcceleratingCurve Then
                    tLen = 256 * buffer(1) + buffer(2) 'WARNING: it must be 1004
                    tCmdCode = CmdAcceleratingCurve '0FH
                    tPacketLen = tLen + 2
                Else
                    tLen = buffer(1)
                    tCmdCode = buffer(2)
                    tPacketLen = tLen + 1
                End If
                                
                'TODO:wait to end of packet
                If UBound(buffer) < tPacketLen Then
                    Exit Sub
                End If
                
                
                'copy packet data to packetData buffer
                Dim packetData() As Byte
                Dim dataSum As Long
                dataSum = 0
                ReDim packetData(tPacketLen) '(0->6)
                Dim i
                For i = 0 To tPacketLen
                    packetData(i) = buffer(i)
                    If i > 0 And i < tPacketLen Then
                        dataSum = dataSum + buffer(i)
                    End If
                Next i
                
                'TODO: add packet Data to IncomPacket
                Dim isValidCheckSum  As Boolean
                isValidCheckSum = True
                If packetData(tPacketLen) <> (256 - (dataSum Mod 256)) Then
                    isValidCheckSum = False
                    WriteLog "Invalid Check Sum [" & ByteArrayToString(packetData) & "] => Resend command" & tCmdCode
                    prSendingQueue.Add tCmdCode
                End If
                
                If isValidCheckSum Then
                    prInComePackets.Add packetData
                End If
                
                'TODO: reset buffer and get new packet
                If UBound(buffer) = tPacketLen Then
                    inputBuffer = Empty
                    IsNewPacketArried = True
                    isCommBusy = False
                    Exit Sub
                Else
                        
                    For i = tPacketLen + 1 To UBound(buffer) - 1
                       buffer(i) = buffer(i + 1)
                    Next
                   
                    ReDim Preserve buffer(0 To UBound(buffer) - tLen)
                    inputBuffer = buffer
                    IsNewPacketArried = False
                    Exit Sub
                End If
                
                'Call HandleInput(InBuff)
            Case comEvSend ' There are SThreshold number of
                           ' characters in the transmit buffer.
            Case comEvEOF  ' An EOF character was found in the
                           ' input stream.
         End Select
         
         isCommBusy = False
End Sub

'--------------End Event Handle Functions -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

Sub HandleInput(data() As Byte)
    Dim tmpAddress, dataLength, commandCode
    Dim data1, data2 '..dataN
    Dim checkSum As Long
    Dim is0FCmd As Boolean
     Dim bIndex As Integer
        
    WriteLog "HandleInput Len (" & UBound(data) & ")>> " & ByteArrayToString(data)
    
    
    bIndex = 0
    tmpAddress = data(0)
    WriteLog "Incomming Address" & tmpAddress
    If tmpAddress <> prAddress Then
        WriteLog "INVALID RESPONSE"
        Exit Sub      ' return
    End If
    
    If data(1) = &H3 And data(2) = &HEC And data(3) = CmdAcceleratingCurve Then
        is0FCmd = True
    Else
        is0FCmd = False
    End If
     
    
    If is0FCmd Then
        dataLength = data(1) * 256 + data(2) 'Data Length is dual byte when command code is 0F
        commandCode = data(3)
        bIndex = bIndex + 3
    Else
        dataLength = data(1)
        commandCode = data(2)
        bIndex = bIndex + 2
    End If
        
    'TODO: calculate check sum
            
        
    Select Case commandCode
        Case CmdNonSignal
            
            WriteLog "Non Response Sign 0FFH"
            
        Case CmdGetingStatus
             WriteLog "Status" & data(bIndex + 1)
             Dim status As Byte
             status = data(bIndex + 1)
             prCurrentScreen = status
             
             Select Case status
                Case 0
                    WriteLog "At ***Stable***Screen"
                    screenName = SCREEN_STABLE_NAME
                    'prSendingQueue.Add CmdGetingStableStatus  'SendCmd (CmdGetingStableStatus)
                    'prSendingQueue.Add CmdGetingAccelarateVal 'SendCmd (CmdGetingAccelarateVal)
                    'prSendingQueue.Add CmdMainAccelerating
                Case 1
                    WriteLog "At ***Warm up***Screen"
                    screenName = SCREEN_WARMUP_NAME
                    prSendingQueue.Add CmdGetingStatus
                Case 2
                    WriteLog "At ***Mesure***Screen"
                    screenName = SCREEN_MESURE_NAME
                Case 3
                    WriteLog "At ***Accelerate***Screen"
                    'prSendingQueue.Add CmdAcceleratingCurve
                Case 4
                    WriteLog "At ***Menu***Screen"
                    screenName = SCREEN_MENU_NAME
                    prSendingQueue.Add CmdChangeAccelerateScr
                    prSendingQueue.Add CmdGetingStatus
                Case 5
                    WriteLog "the accelerate data meet stadard requirement"
                Case 6
                    WriteLog "When is start at ***Stable*** Screen, key is `stop`"
                Case 7
                    WriteLog "the way of accelerate is Manual Accelerate"
                Case 8
                    WriteLog "Unknow screen Status =8"
                    screenName = SCREEN_ACCELERATE_NAME
                    prSendingQueue.Add CmdGetingAccelarateVal
                    'prSendingQueue.Add CmdAcceleratingCurve
                Case Else
                    WriteLog "Unknow screen 1111"
                    
             End Select
             
             SetTag "MeasureScreen", screenName
             '''TODO: Process Response here
        Case CmdGetingMesuringVal
             Dim NValueHighBit, NValueLowBit, t1, t2, RpmHighBit, RpmLowBit
             NValueHighBit = data(bIndex + 1)
             NValueLowBit = data(bIndex + 2)
             t1 = data(bIndex + 3)
             t2 = data(bIndex + 4)
             RpmHighBit = data(bIndex + 5)
             RpmLowBit = data(bIndex + 6)
             WriteLog "Cmd " & CmdGetingMesuringVal & ": N value high bit " & NValueHighBit _
                                                    & ", N value low bit " & NValueLowBit _
                                                    & ", T " & t1 _
                                                    & ", T " & t2 _
                                                    & ", rpm high bit  " & RpmHighBit _
                                                    & ", rpm low bit  " & RpmLowBit
             ' Set data to tag
             
             'SetTag "Min1", RpmLowBit
             'SetTag "Max1", RpmHighBit
             
             'SetTag "Opacity1", NValueHighBit * 256 + NValueLowBit
                                                
        Case CmdGetingStableStatus
             Dim NmValueHighBit, NmValueLowBit, MaxTachoH, MaxTachoL
             NmValueHighBit = data(bIndex + 1)
             NmValueLowBit = data(bIndex + 2)
             MaxTachoH = data(bIndex + 3)
             MaxTachoL = data(bIndex + 4)
             WriteLog "Cmd " & CmdGetingStableStatus & ": Nm value high bit " & NValueHighBit _
                                                     & ", Nm value low bit " & NValueLowBit _
                                                     & ", Max tacho H " & MaxTachoH _
                                                     & ", Max tacho L " & MaxTachoL
                                                     
             
             'SetTag "Max3", NValueHighBit * 256 + NValueLowBit
             'SetTag "Opacity3", MaxTachoH * 256 + MaxTachoL
             
        Case CmdGetingAccelarateVal
            Dim NoOfTheLargestAccelerate
            If dataLength = &H5 Then
                NoOfTheLargestAccelerate = data(bIndex + 1)
                WriteLog "Cmd " & CmdGetingAccelarateVal & ": No. of the largest accelerate " & NoOfTheLargestAccelerate
                
                iMeasureTimes = NoOfTheLargestAccelerate
                SetTag "MeasureTimes", NoOfTheLargestAccelerate
                ' Performance new action
                SetTag "Action", ACTION_START
                prSendingQueue.Add CmdGetingAccelarateVal
                Dim iii
                For iii = 1 To Nresult
                    SetTag "Nvalue" & iii, 0
                    SetTag "KValue" & iii, 0
                Next iii
                
            ElseIf dataLength = &H3 Then
                WriteLog "Cmd " & CmdGetingAccelarateVal & ": in the process of accelerating, it's unable to get the data: " & data(bIndex + 1)
                prSendingQueue.Add CmdGetingAccelarateVal
                
                SetTag "Action", ACTION_KEEP
            Else
                
                'TODO: getting accelerate data
                Dim m ' number of group
                Dim i, strOut, N, K
                NoOfTheLargestAccelerate = data(bIndex + 1)
                strOut = "Cmd " & CmdGetingAccelarateVal & ": No. of the largest accelerate " & NoOfTheLargestAccelerate
                m = (NoOfTheLargestAccelerate - 3) / 2
                
                Dim j
                j = bIndex + 1 + 1
                For i = 0 To m / 2
                    strOut = strOut & ",N(" & m - i & ") High =" & data(j) & ",N(" & m - i & ") Low = " & data(j + 1)
                    N = data(j) * 265 + data(j + 1)
                    'SetTag "NValue" & i + 1, N
                    'SetTag "KValue" & i + 1, -ln(1 - CDbl(N) / 100) / 0.43
                    j = j + 1
                Next i
                WriteLog strOut
                
                
                ' Set tag for 3 first value
                
                Dim Uindex, Average
                Average = 0
                'If NoOfTheLargestAccelerate < Nresult Then Nresult = NoOfTheLargestAccelerate
                Uindex = UBound(data)
                For i = 1 To Nresult
                    If i <= NoOfTheLargestAccelerate Then
                        N = data(Uindex - i * 2) * 256 + data(Uindex - (i * 2) + 1)
                        K = -Log(1 - CDbl(N) / 1000) / 0.43
                    Else
                        N = 0
                        K = 0
                    End If
                    
                    SetTag "Nvalue" & i, N
                    SetTag "KValue" & i, K
                    Average = Average + K
                Next i
                SetTag "AverageValue", Average / Nresult
                
                ' stop start action
                SetTag "Action", ACTION_STOP
                ' pause for 5 seconds
                Sleep 5000
                SetTag "Action", ACTION_START
                
                iMeasureTimes = NoOfTheLargestAccelerate
                SetTag "MeasureTimes", NoOfTheLargestAccelerate
                'If iMeasureTimes >= 3 Then
                '    StopRun
                'End If
                
            End If
        Case CmdMainAccelerating
            Dim NmH, NmL
            NmH = data(bIndex + 1)
            NmL = data(bIndex + 2)
            MaxTachoH = data(bIndex + 3)
            MaxTachoL = data(bIndex + 4)
            WriteLog "Cmd " & CmdMainAccelerating & ": NmH =" & NmH & ",MnL=" & NmL & ", Max tachoH=" & MaxTachoH & ",Max tachoL=" & MaxTachoL
            
            SetTag "AverageValue", NmH * 256 + NmL
        Case CmdAcceleratingCurve
            'Dim KiH(), KiL(), N, RpmHigh, RpmLow
            'Dim index As Integer
            'Dim valStr As String
            'KiH = data(bIndex + 1)
            'KiL = data(bIndex + 2)
            'n = 500
            'RpmHigh = data(bIndex + n * 2 + 1)
            'RpmLow = data(bIndex + n * 2 + 2)
            
            'WriteLog "Cmd " & CmdAcceleratingCurve & ": "
            '& ", High Rpm=" & RpmHigh & ",Low Rpm=" & RpmLow
                    
            'For index = 1 To UBound(data)
            '    KiH(index) = data(bIndex + index * 2 - 1)
            '    KiL(index) = data(bIndex + index * 2)
            '    WriteLog ";K" & index & "H=" & KiH(index) & ",K" & index & "L=" & KiL(index)
            '    WriteLog "data" & index & data(bIndex + index)
            'Next index
            
            
        Case CmdOilTempValue
               WriteLog "Cmd " & CmdOilTempValue & ": oil temp value is one byte and unit is Celsius  " & data(bIndex + 1)
        Case Else
            WriteLog "Unknown Command: " & Hex$(commandCode)
    End Select
    
End Sub

Private Function IsStartOfPacket() As Boolean
    If inputBuffer = Empty Then
        IsStartOfPacket = True
    Else
        IsStartOfPacket = False
    End If
    
End Function

Private Function IsEndOfPacket() As Boolean

End Function


Private Function joinArray(ByVal arr1 As Variant, ByVal arr2 As Variant) As Variant
        
        Dim firstArr() As Byte
        Dim secondArr() As Byte
        Dim retArr() As Byte
        firstArr = arr1
        secondArr = arr2
        ReDim retArr(0 To UBound(firstArr) + UBound(secondArr) + 1)
        Dim i
        For i = 0 To (UBound(firstArr) + UBound(secondArr) + 1)
            If i <= UBound(firstArr) Then
                retArr(i) = firstArr(i)
            Else
                retArr(i) = secondArr(i - UBound(firstArr) - 1)
            End If
        Next i
        
        joinArray = retArr
        
        
End Function
